<?php

namespace app\common\model\message;

use think\Model;
use traits\model\SoftDelete;

class Review extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'message_review';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'junior',
        'createtime_text',
        'child',
    ];
    public function getChildAttr($v, $d)
    {
        return self::where('msg_id', $d['id'])->field('id,content')->find() ?? [];
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getJuniorAttr($value, $data)
    {
        return self::all(['p_id' => $data['id']]);
    }
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function message()
    {
        return $this->belongsTo('app\common\model\Message', 'm_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
